﻿using Application_Core.Monitor_Component.Access_Layer;
using Application_Core.Monitor_Component.Access_Layer.Implementations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utility.Global.DataTypes;
using Utility.RabbitMQ_Service;
using Utility.RabbitMQ_Service.Implementations;

namespace MPS_Monitoring_System
{
    public partial class Dashboard : Form
    {
        private RabbitMQService rabbitMQService;
        private MonitorService monitorService;
        private DataTable dataTable;

        public Dashboard()
        {
            InitializeComponent();
            this.monitorService = new MonitorComponentFacade();

            Thread monitorThread = new Thread(new ThreadStart(this.monitorService.StartIAmAliveHandler));

            this.dataTable = this.CreateTable();
            this.dataGridView1.DataSource = this.dataTable;
            monitorThread.Start();
        }

        private void Dashboard_Load(object sender, EventArgs e)
        {
            // Start Button für alle MPS hinzufügen
            DataGridViewButtonColumn btnStart = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btnStart);
            btnStart.HeaderText = "Start MPS";
            btnStart.Text = "Start";
            btnStart.Name = "btnStart";
            btnStart.UseColumnTextForButtonValue = true;

            //Stop Button für alle MPS hinzufügen
            DataGridViewButtonColumn btnStop = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btnStop);
            btnStop.HeaderText = "Stop MPS";
            btnStop.Text = "Stop";
            btnStop.Name = "btnStop";
            btnStop.UseColumnTextForButtonValue = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Starte MPS
            if (e.ColumnIndex == 6)
            {
                string mps = dataGridView1.Rows[e.RowIndex].Cells[0].Value as string;
                monitorService.StartSystem(mps);
            }
            // Stoppe MPS
            else if (e.ColumnIndex == 7)
            {
                string mps = dataGridView1.Rows[e.RowIndex].Cells[0].Value as string;
                monitorService.StopSystem(mps);
            }
        
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //Status Zelle formatieren
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Status")
            {
                if (e.Value != null)
                {
                    if (e.Value as string == "Offline")
                    {
                        e.CellStyle.BackColor = Color.Red;
                        e.CellStyle.ForeColor = Color.White;
                    }
                    else if (e.Value as string == "Working")
                    {
                        e.CellStyle.BackColor = Color.Yellow;
                        e.CellStyle.ForeColor = Color.Black;
                    }
                    else if (e.Value as string == "Online")
                    {
                        e.CellStyle.BackColor = Color.Green;
                        e.CellStyle.ForeColor = Color.Black;
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Jede Sekunde wird der aktuelle Stand abgefragt und für alle MPS Systeme die dataTable aktualisiert
            int i = 0;
            foreach (SystemTyp system in this.monitorService.FindSystemStatus())
            {

                DataRow row = this.dataTable.Rows[i];
                row.BeginEdit();
                string status = system.Status.ToString();
                row["Status"] = status;
                row["Disconnects"] = system.AnzahlDisconnected.ToString();
                row["AbgearbeiteteAnfragen"] = system.AnzahlVerarbeiteteAnfragen.ToString();
                TimeSpan duration = DateTime.Now - system.LetztesUpdate;
                if (status == "Offline")
                {

                    row["OfflineZeit"] = string.Format("{0:D2}:{1:D2}:{2:D2}", duration.Hours, duration.Minutes, duration.Seconds);
                    row["OnlineZeit"] = "0";
                }
                else
                {
                    row["OfflineZeit"] = "0";
                    row["OnlineZeit"] = string.Format("{0:D2}:{1:D2}:{2:D2}", duration.Hours, duration.Minutes, duration.Seconds);
                }
                row.EndEdit();

                if (status == "Offline")
                {
                    dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Red;
                    dataGridView1.Rows[i].Cells[1].Style.ForeColor = Color.White;
                }
                else if (status == "Working")
                {
                    dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Yellow;
                    dataGridView1.Rows[i].Cells[1].Style.ForeColor = Color.Black;
                }
                else if (status == "Online")
                {
                    dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Green;
                    dataGridView1.Rows[i].Cells[1].Style.ForeColor = Color.Black;
                }
                //
                //row.cancelEdit();
                i++;
            }
        }

        public DataTable CreateTable()
        {
            DataTable d = new DataTable();

            //MPS Daten abrufen und als DataTable speichern
            d.Columns.Add("MPS");
            d.Columns.Add("Status");
            d.Columns.Add("Disconnects");
            d.Columns.Add("AbgearbeiteteAnfragen");
            d.Columns.Add("OnlineZeit");
            d.Columns.Add("OfflineZeit");

            foreach (SystemTyp system in this.monitorService.FindSystemStatus())
            {
                string[] row = new string[] { system.Name, system.Status.ToString(), system.AnzahlDisconnected.ToString(), system.AnzahlVerarbeiteteAnfragen.ToString(), "0", "0" };
                d.Rows.Add(row);
            }
            return d;
        }
    }
}
